Attribute VB_Name = "modDerived"
'***************************************************************
' (c) Copyright 2000 Matthew J. Curland
'
' This file is from the CD-ROM accompanying the book:
' Advanced Visual Basic 6: Power Techniques for Everyday Programs
'   Author: Matthew Curland
'   Published by: Addison-Wesley, July 2000
'   ISBN: 0-201-70712-8
'   http://www.PowerVB.com
'***************************************************************
Option Explicit

Private Const cVTableSize As Long = 11
Private Type WrapVTable
    VTable(cVTableSize - 1) As Long
End Type
Private m_VTable As WrapVTable
Private m_pVTable As Long

Public Type DerivedOverride
    BD As BlindDelegator
    Derived As Derived
End Type

Public Sub HookVTable(pBDVTable As Long)
    If m_pVTable = 0 Then
        With m_VTable
            CopyMemory .VTable(0), ByVal pBDVTable, 4 * cVTableSize
            .VTable(10) = FuncAddr(AddressOf OverrideMe)
            m_pVTable = VarPtr(.VTable(0))
        End With
    End If
    pBDVTable = m_pVTable
End Sub
Public Function DestructDerivedOverride(This As DerivedOverride, pInner As IUnknown, pOuter As IUnknown) As Long
    'Clear Derived so that we don't leak
    Set This.Derived = Nothing
End Function
Private Function OverrideMe(This As DerivedOverride, retVal As String) As Long
    'Make sure [out] param is NULL
    VBoost.AssignZero retVal
    
    On Error GoTo Error
    'Jump to friend function in derived class
    retVal = This.Derived.Derived_OverrideMe
    Exit Function
Error:
    OverrideMe = MapError
End Function
Private Function FuncAddr(ByVal pfn As Long) As Long
    FuncAddr = pfn
End Function

